package com.youshang520i.springcommonmiddleware.service.impl;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youshang520i.springcommonmiddleware.service.RocketmqService;
import com.youshang520i.springcommonmiddleware.mq.rabbitmq.po.Mail;
import com.youshang520i.springcommonmiddleware.mq.rocketmq.MQTagConstants;
import com.youshang520i.springcommonmiddleware.mq.rocketmq.MQTopicConstants;
import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author youshang
 */
@Log4j2
@Service
public class RocketmqServiceImpl implements RocketmqService {

    @Autowired
    private DefaultMQProducer mqProducer;

    /**
     * 发送MQ消息
     * @param mail
     * @return
     */
    @Override
    public boolean send(Mail mail){
        if (null == mail){
            return false;
        }

        String mailStrJson = JSONObject.toJSONString(mail);
        Message message = new Message(MQTopicConstants.TOPIC_YOUSHANG, MQTagConstants.TAG_YOUSHANG_LEARNING, UUID.fastUUID().toString(),mailStrJson.getBytes());

        try {
            //设置延迟发送
//            message.setDelayTimeLevel(2);
            mqProducer.send(message);
            return true;
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 测试发送广播模式
     * @param mail
     * @return
     */
    @Override
    public boolean sendBroadcast(Mail mail){
        if (null == mail){
            return false;
        }

        String mailStrJson = JSONObject.toJSONString(mail);
        Message message = new Message(MQTopicConstants.TOPIC_BROADCAST, MQTagConstants.TAG_BROADCAST_1, UUID.fastUUID().toString(),mailStrJson.getBytes());

        try {
            //设置延迟发送
            message.setDelayTimeLevel(2);
            mqProducer.send(message);
            log.info("broadcasr 发送成功 : " + mailStrJson);
            return true;
        } catch (MQClientException e) {
            e.printStackTrace();
        } catch (RemotingException e) {
            e.printStackTrace();
        } catch (MQBrokerException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 接受MQ消息
     * @param str
     * @return
     */
    @Override
    public Mail receive(String str){
        if (StrUtil.isEmpty(str)) {
            return null;
        }
        log.info("MQ接受结果： "+ str);
        return JSON.parseObject(str, Mail.class);
    }


}
